module Syllogism (getSentences) where

import PGF
import Data.Maybe
import Debug.Trace
import Structures

getSentences :: Tree -> Syllogism
getSentences = map getSentence . findTypeOfSentences . nextExprs


getSentence :: (CId,[Expr]) -> Sentence
getSentence (typ,expr) = makeSentence (show typ) (getSubject expr) (getProperty expr)

makeSentence :: String -> SSubject -> SProperty -> Sentence
makeSentence x s p = case x of
   "SoP" -> SoP s p
   "SaP" -> SaP s p
   "SiP" -> SiP s p
   "SeP" -> SeP s p
   "CnP" -> CnP s p
   "CpP" -> CpP s p
   y     -> error $ y ++ " " ++ show s ++ " " ++ show p


findTypeOfSentences []     = []
findTypeOfSentences (x:xs) = case unApp x of
   Just (cId, expr) -> if findAppSentences (showCId cId)
                         then getSentenceApp expr ++ conc xs
                          else getSentenceMk expr ++ conc xs
--   where conc = 

conc [] = [] -- error "no conclusion given"
conc (x:xs) = case unApp x of 
   Just (cId,expr) -> if showCId cId == "ToConclusion" 
                            then (getSentenceMk (nextExprs x))
                              else error "no conclusion given" --  : findTypeOfSentences xs 
   _               -> error "no conclusive" 
--difference between getSentenceApp and findTypeOf???

getSentenceApp :: [Expr] -> [(CId, [Expr])]
getSentenceApp [] = error "getSentenceApp error"
getSentenceApp (x:xs) =  case unApp x of
   Just (cId, expr) -> if findTypeOfSentence (showCId cId) then (cId,expr) : findTypeOfSentences xs
                         else findTypeOf xs
   _                -> error $ "Can't use unApp on: " ++ show x


findTypeOf :: [Expr] -> [(CId, [Expr])]
findTypeOf [] = []
findTypeOf (x:xs) = case unApp x of
   Just (cId, expr) -> if findTypeOfSentence (showCId cId) then (cId,expr) : findTypeOfSentences xs
                         else findTypeOf xs
   _                -> error $ "Can't use unApp on: " ++ show x

getSentenceMk [] = []
getSentenceMk (x:xs)  =  case unApp x of
   Just (cId, expr) -> if findTypeOfSentence (showCId cId) then (cId,expr) : getSentenceMk xs
                         else findTypeOf xs
   _                -> error $ "Can't use unApp on: " ++ show x

getSubject :: [Expr] -> SSubject
getSubject [] = error "empty list in getSubject"
getSubject (x:xs) = case unApp x of
  Nothing          -> getSubject xs -- innan gjordes error, men vi får problem med EMeta som ger Nothing om man gör unApp.
  Just (cId,expr)  -> case show cId of
     "ToSubject"   -> Sub (show $ getSubjOrProp expr)
     "ToConstant"  -> Sub (show $ getSubjOrProp expr)
     "SPhrase"     -> SPhrase (phrase $ head expr) (phrase $ last expr)
     s             -> getSubject xs


  
getSubject' :: [Expr] -> String -> Maybe CId
getSubject' []     s = Nothing
getSubject' (x:xs) s = case unApp x of
   Just (cId, expr) -> if showCId cId == s then Just (getSubjOrProp expr) else getSubject' xs s
   _                -> error $ "Can't use unApp on: " ++ show x


getProperty :: [Expr] -> SProperty
getProperty []     = error "empty list in getProperty"
getProperty (x:xs) = case unApp x of
   Nothing         -> getProperty xs --error "no property"
   Just (cId,expr) -> case show cId of
      "ToProperty" -> Prop (show $ getSubjOrProp $ nextExprs $ head expr)
      "Or"         -> Or  (fst operands) (snd operands)
      "And"        -> And (fst operands) (snd operands)
      "PPhrase"    -> PPhrase (phrase $ head expr) (phrase $ last expr)
      s            -> getProperty xs
      where func   = show . getSubjOrProp . nextExprs
            operands = case length expr of
               2 -> (func (head expr),func (last expr))
               _ -> error "More than two conjuncts/disjuncts"


getSubjOrProp :: [Expr] -> CId
getSubjOrProp = nextFunction . head . nextExprs . head


phrase :: Expr -> String
phrase = show . nextFunction

nextExprs :: Expr -> [Expr]
nextExprs = snd . fromJust . unApp
nextFunction :: Expr -> CId
nextFunction = fst . fromJust . unApp
 

findAppSentences :: String -> Bool
findAppSentences "appSentences" = True
findAppSentences "mkSentences"  = False 
findAppSentences _              = error "Wrong in findAppSentences"


findTypeOfSentence :: String -> Bool
findTypeOfSentence = flip elem ["SaP", "SiP", "SoP","SeP","CnP","CpP"]



